package CtrLayer;

import java.util.ArrayList;
import ModelLayer.Lease;
import ModelLayer.LeaseCollection;
import ModelLayer.Machine;
import ModelLayer.MachineCollection;

/**
 * Write a description of class LeaseCtr here.
 * 
 * @author (your name)
 * @version (a version number or a date)
 */
public class LeaseCtr
{

	/**
	 * Constructor for objects of class LeaseCtr
	 */
	public LeaseCtr()
	{
	}

	public String create_lease(String return_date, ArrayList<String> ids, Double price)
	{
		return LeaseCollection.create_lease(return_date, ids, price);
	}

	public void close_order(String id, double recalculate_cost)
	{
		LeaseCollection.close_lease(id, recalculate_cost);
	}
	
	public Object[] display_lease_id(String id)
	{
		Object[] leases = new Object[6];
		Lease lease=LeaseCollection.find_by_id(id);
		leases=lease.give_array();
		return leases;
	}
	
	public Object[][] display_all_leases()
	{
		return LeaseCollection.display_all_leases();
	}

	public ArrayList<Object[]> lease_machine_ids(String id)
	{
		
		ArrayList<String> temp=new ArrayList<>();
		ArrayList<Object[]> machines=new ArrayList<>();
		
		temp=LeaseCollection.find_by_id(id).get_ids();
		for (String t:temp)
		{
			machines.add(MachineCollection.find_by_id(t).give_array());
		}
		return machines;
	}
	public boolean add_machine(String name, double price_per_month)
	{
		return MachineCollection.add_machine(name, price_per_month);
	}

	public void save_data()
	{
		MachineCollection.save_data();
		LeaseCollection.save_data();
	}

	public void edit_machine(String id, String name, double price_per_month)
	{
		MachineCollection.edit_machine(id, name, price_per_month);
	}

	public boolean delete_machine(String id)
	{
		LeaseCollection.clear_machine(id);
		return MachineCollection.delete_machine(id);
	}

	public Object[][] display_all_machines()
	{
		int i=0;
		ArrayList<Machine> machines = MachineCollection.get_machines();
		Object[][] store = new Object[machines.size()][5];
		for (Machine temp: machines)
		{Object[] local = temp.give_array();
		if (temp.get_price_per_day()==0) 
		{
			local[4]="Unfinished";
		}
		else
		{
			local[4]="Finished";
		}
		store[i]=local;
		i++;
		}
		return store;
	}
	public Object[][] display_fine_machines()
	{
		int i=0;
		ArrayList<Machine> machines = MachineCollection.get_machines();
		Object[][] store = new Object[machines.size()][4];
		for (Machine temp: machines)
		{Object[] local = temp.give_array();
		if ((temp.get_price_per_day()!=0)&&(local[3].equals("Available"))) 
		{store[i]=new Object[]{local[0],local[1],local[2]};
		i++;
		}
		}
		Object[][] newstore = new Object[i][3];
		for (int j=0;j<i;j++)
		{
			newstore[j]=store[j];
		}
		return newstore;
	}
	
	public Object[] display_machine_id(String id)
	{
		Object[] machines = new Object[6];
		Machine machine=MachineCollection.find_by_id(id);
		machines=machine.give_array();
		if (machine.get_price_per_day()==0) 
		{
			machines[4]="Unfinished";
		}
		else
		{
			machines[4]="Finished";
		}
		return machines;
	}
	
	public Object[] display_machine_name(String name)
	{
		ArrayList<Machine> machines = MachineCollection.get_machines();
		for (Machine machine: machines)
		{
			
			if (machine.get_name().equals(name))
			{
				return display_machine_id(machine.get_id());
			}
		}
		return null;
	}
	public String get_machine_name(String id)
	{
		return MachineCollection.find_by_id(id).get_name();
	}

	public String[] display_machine(String id)
	{
		return new String[]{MachineCollection.find_by_id(id).get_name(),""+MachineCollection.find_by_id(id).get_price_per_day()};
	}

	public boolean is_such_machine_id(String id)
	{
		return MachineCollection.is_such_id(id);
	}

	public boolean is_such_lease_id(String id)
	{
		return LeaseCollection.is_such_id(id);
	}

	public boolean is_lease_closed(String id)
	{
		return LeaseCollection.is_lease_closed(id);
	}
	
	public boolean is_such_machine_name(String name)
	{
		ArrayList<Machine> machines = MachineCollection.get_machines();
		for (Machine machine: machines)
		{
			if (machine.get_name().equals(name))
			{
				return true;
			}
		}
		return false;
	}
	
	public void finish_machine(String id, double price_per_day)
	{
		MachineCollection.finish_machine(id, price_per_day);
	}
}
